package com.example.demo.controller;

import com.example.demo.componet.ReadCSV;
import com.example.demo.model.BrandJson;
import com.example.demo.model.YearJson;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import scala.Tuple2;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.swing.*;
import java.io.Serializable;
import java.util.*;

@RestController
@RequestMapping("/analyze")
public class MonthSaleController implements Serializable {

    @Resource
    private JavaSparkContext sc;

    //文件地址
    @Autowired
    private String getFilePath;

    private List<ArrayList> getMaps() {
        ReadCSV readCSV = new ReadCSV();

        //List<ArrayList> maps =
          return  readCSV.readYearPrice(getFilePath, 1, 7);  //"C:\\Users\\fireapproval\\Desktop\\毕业设计\\数据集\\test.csv"

    }
    @GetMapping("/monthSale")
    public List<BrandJson> method1(){

        List<Integer> one = new ArrayList<>() ;
        List<Integer> two = new ArrayList<>();
        List<Integer> three = new ArrayList<>();
        List<Integer> four = new ArrayList<>();

        List<BrandJson> brandJsons =new ArrayList<>();

        for (Object obj:getMaps()
        ) {
            ArrayList a = (ArrayList) obj;

            Integer i = Integer.parseInt((String) a.get(0)) ;
            //System.out.println(a.get(0)+"----"+a.get(1));
            //System.out.println(a.get(0)+"--"+a.get(0).getClass());  //4--class java.lang.String
            //System.out.println(a.get(1)+"--"+a.get(1).getClass());
            Float f = (Float) a.get(1);

            if ( 1<= i.intValue() && i.intValue()<=3){
                one.add(f.intValue());
            }
            if (4<= i.intValue() && i.intValue() <=6){
                two.add(f.intValue());
            }
            if (7<= i.intValue() && i.intValue() <=9){
                three.add(f.intValue());
            }
            if (10<= i.intValue() && i.intValue() <=12){
                four.add(f.intValue());
            }


        }
        //
        one.add(0);one.add(0);two.add(0);two.add(0);three.add(0);three.add(0);four.add(0);four.add(0);

        //RDD

        JavaRDD<Integer> rdd = sc.parallelize(one);
        JavaRDD<Integer> rdd1 = sc.parallelize(two);
        JavaRDD<Integer> rdd2 = sc.parallelize(three);
        JavaRDD<Integer> rdd3 = sc.parallelize(four);

        Integer reduce = rdd.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);
        Integer reduce1 = rdd1.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);
        Integer reduce2 = rdd2.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);
        Integer reduce3 = rdd3.reduce((Function2<Integer, Integer, Integer>) (integer, integer2) -> integer + integer2);

        BrandJson brandJson = new BrandJson();brandJson.setName("第一季度");brandJson.setValue(reduce);
        BrandJson brandJson1 = new BrandJson();brandJson1.setName("第二季度");brandJson1.setValue(reduce1);
        BrandJson brandJson2 = new BrandJson();brandJson2.setName("第一季度");brandJson2.setValue(reduce2);
        BrandJson brandJson3 = new BrandJson();brandJson3.setName("第一季度");brandJson3.setValue(reduce3);

        brandJsons.add(brandJson);brandJsons.add(brandJson1);brandJsons.add(brandJson2);brandJsons.add(brandJson3);

        return brandJsons;
    }
}
